package com.gxgy.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gxgy.base.BaseAllInterface;
import com.gxgy.entity.Role;
import com.gxgy.entity.User;
import com.gxgy.uilt.IfVersionException;
import com.gxgy.uilt.MD5Util;

/**
 * @description: user表接口
 * @author: 邓忠柠
 * @date: 2021-11-17
 */
@Controller
public class UserController extends BaseAllInterface {
	@GetMapping("/index")
	public String index() {
		return "index";
	}

	/**
	 * @description: user表接口
	 * @author: 邓忠柠
	 * @date: 2021-11-17
	 */
	@RequestMapping("/setting")
	public String main() {
		return "page/setting";

	}

	/**
	 * @description: 用户列表
	 * @author: 邓忠柠
	 * @date: 2021-11-17
	 */
	@RequestMapping("/userList")
	public String findAllUser(Model model) {
		List<User> list = userService.queryAllUser();
		model.addAttribute("user", list);

		List<Role> role = roleService.queryAllRole();
		model.addAttribute("roleList", role);
		return "page/user/user";

	}

	/**
	 * @description: 用户添加页面
	 * @author: 邓忠柠
	 * @date: 2021-11-17
	 */
	  @RequestMapping("/userAdd") public String add(Model model) { 
		  List<Role> list = roleService.queryAllRole(); 
		  model.addAttribute("role",list); 
	     return"page/user/userAdd";
	  }
	  
	  @RequestMapping("/doAdd")
	  public String doAdd(String name,String username,String sex,String status,String password,String date,Model model) {
		if (name.isEmpty()) {
			model.addAttribute("msg","昵稱不能为空");
			return"page/user/userAdd";
		}
		if (username.isEmpty()) {
			model.addAttribute("msg","用户名不能为空");
			return"page/user/userAdd";
		}
		if (sex.isEmpty()) {
			model.addAttribute("msg","性别不能为空");
			return"page/user/userAdd";
		}
		if (status.isEmpty()) {
			model.addAttribute("msg","身份不能为空");
			return"page/user/userAdd";
		}
		if (password.isEmpty()) {
			model.addAttribute("msg","密码不能为空");
			return"page/user/userAdd";
		}
		if (date.isEmpty()) {
			model.addAttribute("msg","日期不能为空");
			return"page/user/userAdd";
		}

		User returnUser = userService.queryUserByUserName(username);
		if (returnUser != null) {
			model.addAttribute("msg","该用户名已存在");
			throw new IfVersionException("usename已存在，该用户名已存在");
		}
		userService.addOneUser(name, username, sex, password, status, date, model);
		  return "redirect:userList";
	}
	  
	  /**
		 * @description: 用户更新页面
		 * @author: 邓忠柠
		 * @date: 2021-11-17
		 */
	  @RequestMapping("/userEdit")
	  public String edit(String id,Model model) {
		  User user = userService.queryUserById(id);
		  MD5Util.md5Encryption(user.getPassword()); 
		  model.addAttribute("user",user);
		  List<Role> list = roleService.queryAllRole(); 
		  model.addAttribute("role",list); 
		return "page/user/userEdit";
	}
	  
	  @RequestMapping("/doEdit")
	  public String doEdit(String id, String name,String username,String sex,String status,String password,String date,Model model) {
		  if (name.isEmpty()) {
				model.addAttribute("msg","昵稱不能为空");
				return"page/user/userEdit";
			}
			if (username.isEmpty()) {
				model.addAttribute("msg","用户名不能为空");
				return"page/user/userEdit";
			}
			if (sex.isEmpty()) {
				model.addAttribute("msg","性别不能为空");
				return"page/user/userEdit";
			}
			if (status.isEmpty()) {
				model.addAttribute("msg","身份不能为空");
				return"page/user/userEdit";
			}
			if (password.isEmpty()) {
				model.addAttribute("msg","密码不能为空");
				return"page/user/userEdit";
			}
			if (date.isEmpty()) {
				model.addAttribute("msg","日期不能为空");
				return"page/user/userEdit";
			}

			User returnUser = userService.queryUserByUserName(username);
			if (returnUser != null) {
				model.addAttribute("msg","该用户名已存在");
				throw new IfVersionException("usename已存在，该用户名已存在");
			}
			userService.updateUser(id, name, username, sex, password, status, date, model);
		return "redirect:userList";
	}
	 
	  /**
		 * @description: 基本资料页
		 * @author: 邓忠柠
		 * @date: 2021-11-17
		 */
	  @RequestMapping("/userSetting")
	  public String userSetting(String id,Model model) {
		  
		return "page/user-setting";
		
	}
}
